package anonymous;

import java.util.ArrayList;
import java.util.Collections;
import rescuecore2.worldmodel.EntityID;
import rescuecore2.standard.entities.*;
import rescuecore2.log.Logger;
public class TaskList{
	private ArrayList<Task> taskList;

	public TaskList(){
		//taskList = (ArrayList<Task>)Collections.synchronizedList(new ArrayList<Task>()); THAT KILLS THE KERNEL NO MATTER IF YOU USE IT OR NOT !!!! 
		taskList = new ArrayList<Task>();
	}

	TaskList(TaskList tl){
		this.taskList = new ArrayList<Task>();
		for(Task t : tl.getList()){
			this.taskList.add(new Task(t));
		}
	}

	public void add(Task new_t){
		Task toBeDeleted = null;
		for(Task t : taskList){
			if(t.eID == new_t.eID){
				toBeDeleted = t;
				break;
				//remove duplicates
				//and keeps the last one
			}
		}
		if(toBeDeleted != null)
			taskList.remove(toBeDeleted);
		taskList.add(new_t); 
		Collections.sort(taskList);
	}

	public boolean remove(Task t){
		int index = taskList.indexOf(t);
		if(index == -1)
			return false;
		else
			taskList.remove(index);
		return true;
	}

	public boolean remove(EntityID eid){
		for(Task t : taskList){
			if(t.eID == eid){
				return taskList.remove(t);
			}
		}
		return false;
	}

	public Task top(StandardWorldModel m){
		/*
		for(StandardEntity thing : m.getEntitiesOfType(StandardEntityURN.CIVILIAN,StandardEntityURN.BUILDING,StandardEntityURN.BLOCKADE,StandardEntityURN.ROAD)){
			for(Task t : taskList){

				if(t.area.getID() == thing.getID()){
					if( t.area.isBlockadesDefined() )
						taskList.remove(t);
				}
			}
		}
		*/
		return taskList.get(0);
	}

	public Task get(int i){
		return taskList.get(i);
	}

	public boolean isEmpty(){ return taskList.isEmpty(); }

	public ArrayList<Task> getList(){
		return taskList;
	}

	public void print(){
		for(Task t : taskList){
			Logger.debug("PRinting List");
			Logger.debug("ID: "+t.eID + " Area: "+ t.area +"\n");
		}
	}

	public ArrayList<Task> checkForDistantTasks(){ return null;  }
}
